﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace IBS
{
    public partial class IbsDataContext
    {
        public IEnumerable<IBSImportFileFormat> GetIBSImport(long InvoiceID)
        {
            var sql = String.Format(@"
                                      SELECT 
                                             SOL.ORDER_NUM, 
                                             S.SITEIBSCODE,
                                             SO.DOC_NUMBER,
                                             SO.COMPANY_KEY,
                                             S.IP,
                                             SO.DOC_DATE,
                                             SUM (SOL.QUANTITY) QTY,
                                             SOL.ITEM_KEY,
                                             CASE when SOL.ACTUAL_PRICE < SOL.PRICE THEN
                                                SOL.ACTUAL_PRICE
                                             else
                                                SOL.PRICE
                                             end  ACTUAL_PRICE   
                                        FROM edex.WH_SALESORDER SO
                                             LEFT JOIN edex.WH_SALESORDERLINES SOL
                                                ON SO.ID_RECORD = SOL.ID_INVOICE
                                             LEFT JOIN IBSDBO.TBSITE S
                                                ON SO.LOCATION_KEY = S.SITEIBSCODE
                                        WHERE SO.ID_RECORD = {0}
                                    GROUP BY 
                                             SOL.ORDER_NUM, 
                                             S.SITEIBSCODE,
                                             SO.DOC_NUMBER,
                                             SO.COMPANY_KEY,
                                             S.IP,
                                             SO.DOC_DATE,
                                             SOL.ITEM_KEY,
                                             SOL.ACTUAL_PRICE,
                                             SOL.PRICE
                                    ORDER BY SOL.ORDER_NUM", InvoiceID);
            return new DB
                .Oracle(this.connectionString)
                .Excecute<IBSImportFileFormat>(sql);
        }
    }

    public class IBSImportFileFormat
    {
        public Int64 SITEIBSCODE { get; set; }
        public String IP { get; set; }
        public String DOC_NUMBER { get; set; }
        public Int32 COMPANY_KEY { get; set; }
        public DateTime DOC_DATE { get; set; }
        public Int64 ITEM_KEY { get; set; }
        public Int64 QTY { get; set; }
        public Double ACTUAL_PRICE { get; set; }


        public void SerializeToFile(string filePath)
        {
            String spaceCHR = " ";
            StringBuilder resultString = new StringBuilder();
            resultString.Append(String.Concat(ITEM_KEY.ToString(), spaceCHR));
            resultString.Append(String.Concat(QTY.ToString().Replace(",", "."), spaceCHR));
            resultString.Append(ACTUAL_PRICE.ToString().Replace(",", "."));
            File.AppendAllText(filePath, resultString.ToString() + "\r\n");
        }
    }
}
